package com.example.demo.nosql.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.noear.esearchx.EsContext;
import org.noear.esearchx.model.EsData;
import org.noear.solon.annotation.*;

/**
 * @author airhead
 */
@Controller
@Mapping("/es")
@Api("es")
@Slf4j
public class EsController {
  @Inject private EsContext esContext;

  @Mapping
  @Get
  @ApiOperation("获取列表")
  public List<DemoEntity> list() {
    try {
      EsData<DemoEntity> result = null;
      result = esContext.indice("demo").selectList(DemoEntity.class);
      return result.getList();
    } catch (IOException e) {
      log.error("add error", e);
      throw new RuntimeException(e);
    }
  }

  @Mapping
  @Post
  @ApiOperation("保存文档")
  public Boolean add(@Body DemoEntity entity) {
    try {
      esContext.indice("demo").insert(entity);
    } catch (IOException e) {
      log.error("add error", e);
      throw new RuntimeException(e);
    }

    return true;
  }
}
